#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>

int countNum = 0;
// 定义一个线程函数，参数为void *类型的指针
void *thread_function(void *arg){
    int innerCount = 0;
   // 当countNum不等于9时，循环执行
   while(countNum != 9){    
        innerCount++;
        // 打印线程ID和countNum的值
        printf("Thread ID: %ld \n",pthread_self());

        // 线程休眠1秒
        sleep(1);
    }
    printf("Thread ID: %ld ending innerCount: %d\n",pthread_self(),innerCount);
    return NULL;
}
int main(int argc,char *argv[]){

    // 创建一个线程
    pthread_t threadid;
    pthread_create(&threadid,NULL,thread_function,NULL);
    int numi = 0;
    // 循环10次
    while (numi < 10)
    {
        // 打印主线程ID和计数
        printf("===================Main Thread ID: %ld Count: %d\n",pthread_self(),numi);
        numi++;
        // 暂停1秒
        sleep(1);

    }
    countNum = 9;
    // 等待线程结束
    pthread_join(threadid,NULL);
    // 打印主线程ID
    printf("===================Main Thread ID: %ld ending\n", pthread_self());


    return 0;
}